package com.xyb.controller;

import com.xyb.entity.SysMenu;
import com.xyb.service.MenuService;
import com.xyb.vo.SysMenuVo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("menu")
public class MenuController {
    @Resource
    private MenuService menuService;

    /**
     * 查询所有菜单
     * @return
     */
    @GetMapping("/menus")
    public List<SysMenu> selectSysMenu() {
        return menuService.selectSysMenu();
    }

    /**
     * 用户登录查询菜单列标
     * @param userId
     * @return
     */
    @GetMapping("/menus/{userId}")
    public List<SysMenu> selectSysMenuByUserId(@PathVariable("userId") Long userId) {
        return menuService.selectSysMenuByUserId(userId);
    }

    /**
     * 根据父节点创建菜单
     * @param menuPid
     * @param sysMenuVo
     * @return
     */
    @PostMapping("/menu/{pid}")
    public String insertSysMenuByMenuPid(@PathVariable("pid")Long menuPid,@RequestBody @Validated(SysMenuVo.insert.class) SysMenuVo sysMenuVo) {
        sysMenuVo.setMenuPid(menuPid);
        return menuService.insertSysMenuByMenuPid(sysMenuVo);
    }

    /**
     * 根据菜单 id 修改菜单
     * @param menuId
     * @param sysMenuVo
     * @return
     */
    @PutMapping("/menu/{menuId}")
    public String updateSysMenuByMenuId(@PathVariable("menuId")Long menuId,@RequestBody @Validated(SysMenuVo.update.class) SysMenuVo sysMenuVo) {
        sysMenuVo.setMenuId(menuId);
        return menuService.updateSysMenuByMenuId(sysMenuVo);
    }

    @PutMapping("/menu/order")
    public String updateSysMenuShowOrder(@RequestBody Long[] menuIds) {
        return menuService.updateSysMenuShowOrder(menuIds);
    }

    /**
     * 根据菜单id删除
     * @param menuId
     * @return
     */
    @DeleteMapping("/menu/{menuId}")
    public String deleteSysMenuByMenuId(@PathVariable("menuId")Long menuId) {
        return menuService.deleteSysMenuByMenuId(menuId);
    }
}
